package com.bridgeQQ.client.control.commands
{
	import com.bridgeQQ.client.core.ICommand;
	
	import mx.core.ClassFactory;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.Operation;

	public class RemoteMethodCommand extends BaseRespond implements ICommand
	{	
		
		public function RemoteMethodCommand(info:Object, target:Object, ...params)
		{
			super(info, target);
			this.params = params;
		}
		
		public override function execute():void
		{
			var method:Operation = target as Operation;
			method.addEventListener(ResultEvent.RESULT, onResult,false, -1);
			method.addEventListener(FaultEvent.FAULT, onFault, false, -1);
			var send:Function = method.send;
			send.apply(null, this.params);
		}
		
		private function onResult(event:ResultEvent):void
		{
			event.target.removeEventListener(ResultEvent.RESULT, onResult);
			event.target.removeEventListener(FaultEvent.FAULT, onFault);
			if(event.result)
			{
				createRespondInfo(false, event.result);
				return;
			}
			createRespondInfo(true, event.result);
		}
		
		
		private function onFault(event:FaultEvent):void
		{
			event.target.removeEventListener(ResultEvent.RESULT, onResult);
			event.target.removeEventListener(FaultEvent.FAULT, onFault);
			createRespondInfo(true, null);
		}
		
	}
}